﻿using IBll;
using IDal;
using Model;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace Bll
{
    public class EmpBll : IEmpBll
    {
        IBaseDal<Employee> dal;
        IBaseDal<EmyployeeType> dal1;
        public EmpBll(IBaseDal<Employee> dal, IBaseDal<EmyployeeType> dal1)
        {
            this.dal = dal;
            this.dal1 = dal1;
        }

        public Task<int> AddEmp(Employee employee)
        {
            return dal.AddAsync(employee);
        }

        public  Task<int> AddEmpType(EmyployeeType emyployeeType)
        {
            return  dal1.AddAsync(emyployeeType);
        }

        public IQueryable<EmyployeeType> emyployeeTypes()
        {
            return dal1.GetAsync();
        }

        public Employee Find(int id)
        {
            var m = dal.GetAsync().Where(x => x.Id == id).FirstOrDefault();
            return m;
        }

        public object getAll(int pageindex = 1, int pagesize = 3)
        {
           var a = dal.GetAsync();
           var b = dal1.GetAsync();
            var linq = from x in a
                       join s in b
                       on x.StateId equals s.StateId
                       select new EmpDTO()
                       {
                           Id = x.Id,
                           Number = x.Number,
                           Name = x.Name,
                           pwd = x.pwd,
                           Barthday = x.Barthday,
                           Age = x.Age,
                           Img = x.Img,
                           IsDeleted = x.IsDeleted,
                           StateId = x.StateId,
                           Sex = x.Sex,
                           SdateName = s.SdateName

                       };
            var count = linq.Count();
            var page = (int)Math.Ceiling(count*1.0/pagesize);
            var list = linq.OrderBy(x => x.Id).Skip((pageindex-1)*pagesize).Take(pagesize);
            return new
            {
                count,
                list,
                page
            };


        }

        public Task<int> UpdateEmp(Employee employee)
        {
            return dal.UpdateAsync(employee);
        }
    }
}
